package com.zougen202301180355.restaurant.controller;

import com.zougen202301180355.restaurant.pojo.Customer;
import com.zougen202301180355.restaurant.pojo.Order;
import com.zougen202301180355.restaurant.service.CustomersService;
import com.zougen202301180355.restaurant.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author 邹根
 * 订单管理控制器
 */
@Controller
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private CustomersService customersService;

    /**
     * 显示订单列表
     */
    @GetMapping("/list")
    public String list(Model model) {
        List<Order> orders = ordersService.getAllOrders();
        System.out.println("===== 订单列表查询 =====");
        System.out.println("查询到的订单数量: " + (orders == null ? "null" : orders.size()));
        if (orders != null && !orders.isEmpty()) {
            for (Order order : orders) {
                System.out.println("订单: " + order);
            }
        }
        model.addAttribute("orders", orders);
        return "orders/list";
    }

    /**
     * 显示添加订单页面
     */
    @GetMapping("/add")
    public String showAddForm(Model model) {
        model.addAttribute("order", new Order());
        List<Customer> customers = customersService.getAllCustomers();
        model.addAttribute("customers", customers);
        return "orders/add";
    }

    /**
     * 处理添加订单请求
     */
    @PostMapping("/add")
    public String addOrder(@ModelAttribute Order order) {
        order.setOrderDate(new Date());
        ordersService.addOrder(order);
        return "redirect:/orders/list";
    }

    /**
     * 显示编辑订单页面
     */
    @GetMapping("/edit/{id}")
    public String showEditForm(@PathVariable("id") Integer id, Model model) {
        Order order = ordersService.getOrderById(id);
        model.addAttribute("order", order);
        List<Customer> customers = customersService.getAllCustomers();
        model.addAttribute("customers", customers);
        return "orders/edit";
    }

    /**
     * 处理编辑订单请求
     */
    @PostMapping("/edit")
    public String editOrder(@ModelAttribute Order order) {
        ordersService.updateOrder(order);
        return "redirect:/orders/list";
    }

    /**
     * 删除订单
     */
    @GetMapping("/delete/{id}")
    public String deleteOrder(@PathVariable("id") Integer id, Model model) {
        try {
            ordersService.deleteOrder(id);
            return "redirect:/orders/list";
        } catch (Exception e) {
            // 外键约束异常，该订单有订单详情
            if (e.getMessage().contains("foreign key constraint") || 
                e.getCause() != null && e.getCause().getMessage().contains("foreign key constraint")) {
                model.addAttribute("error", "无法删除：该订单已有订单详情，请先删除相关订单详情。");
            } else {
                model.addAttribute("error", "删除失败：" + e.getMessage());
            }
            // 重新加载订单列表
            model.addAttribute("orders", ordersService.getAllOrders());
            return "orders/list";
        }
    }

    /**
     * 查看订单详情(包含顾客信息和订单详情)
     */
    @GetMapping("/detail/{id}")
    public String viewDetail(@PathVariable("id") Integer id, Model model) {
        Order order = ordersService.getOrderWithDetails(id);
        model.addAttribute("order", order);
        return "orders/detail";
    }
}
